home *** CD-ROM | disk | FTP | other *** search
/ Top 200 Programs / Top 200 Programs.iso / Bob8 / THOMPSON / LIBERTY / PRODUCT / TUTORIAL.EXE / WK2CM1.TXT < prev    next >
Text File  |  1996-12-02  |  9KB  |  258 lines

  1. Week 2 course material
  2. Liberty BASIC programming course
  3. Copyright 1996 Shoptalk Systems
  4. All Rights Reserved
  5.  
  6. Using Arrays
  7. ==============================================================================
  8.  
  9. Computers are more often than not used to keep track of lists of things. 
  10. These could be lists of customers, lists of stock #'s, or lists of fonts.  A
  11. list of things doesn't even have to look like a list.  For an example of
  12. this, take a look at your favorite video games.  If you've ever played PacMan
  13. or Doom, the monsters are managed in one kind of list or another (but you
  14. never see the list).  Arrays are what BASIC (and most other programming
  15. languages) uses to keep lists.
  16.  
  17. To see why we need arrays, let's look at what programming for lists looks
  18. like without arrays.  Suppose we want to keep track of a list of 10 names.
  19. Using only the techniques we've covered so far it would look like this:
  20.  
  21.     'NOARRAY.BAS
  22.     'List handling without arrays
  23.  
  24. [askForName]  'ask for a name
  25.     input "Please give me your name ?"; yourName$
  26.     if yourName$ = "" then print "No name entered." : goto [quit]
  27.  
  28.     if name1$ = "" then name1$ = yourName$ : goto [nameAdded]
  29.     if name2$ = "" then name2$ = yourName$ : goto [nameAdded]
  30.     if name3$ = "" then name3$ = yourName$ : goto [nameAdded]
  31.     if name4$ = "" then name4$ = yourName$ : goto [nameAdded]
  32.     if name5$ = "" then name5$ = yourName$ : goto [nameAdded]
  33.     if name6$ = "" then name6$ = yourName$ : goto [nameAdded]
  34.     if name7$ = "" then name7$ = yourName$ : goto [nameAdded]
  35.     if name8$ = "" then name8$ = yourName$ : goto [nameAdded]
  36.     if name9$ = "" then name9$ = yourName$ : goto [nameAdded]
  37.     if name10$ = "" then name10$ = yourName$ : goto [nameAdded]
  38.  
  39.     'There weren't any available slots, inform user
  40.     print "All ten name slots are already used!"
  41.     goto [quit]
  42.  
  43. [nameAdded]   'Notify the name add was successful
  44.  
  45.     print yourName$; " has been added to the list."
  46.     goto [askForName]
  47.  
  48. [quit]
  49.  
  50.     end
  51.  
  52. In this example when we enter a name, the computer will look at up to ten 
  53. variables in turn.  If it finds a variable that is an empty string, it will
  54. set the value of that variable to be equal to yourName$.  Then it will GOTO
  55. [nameAdded] and display a notice that the name has been added.  If this
  56. were part of a larger program, we would execute this code whenever we
  57. wanted to add a name, and the name would be inserted into the next
  58. available slot.
  59.  
  60. This technique is manageable only for very small lists.  Imagine how many
  61. lines of code we would need to track hundreds or thousands of names in this
  62. way!  Arrays provide a special way to make a single line of BASIC code
  63. refer to any one variable out of a list of thousands.
  64.  
  65. Here is our name list example rewritten using a BASIC array.  If it seems
  66. unclear to you, try running the debugger on it.
  67.  
  68.     'ARRAYS.BAS
  69.     'List handling with arrays
  70.  
  71.     dim names$(10)  'set up our array to contain 10 items
  72.  
  73. [askForName]  'ask for a name
  74.     input "Please give me your name ?"; yourName$
  75.     if yourName$ = "" then print "No name entered." : goto [quit]
  76.  
  77.     index = 0
  78. [insertLoop]
  79.     'check to see if index points to an unused item in the array
  80.     if names$(index) = "" then names$(index) = yourName$ : goto [nameAdded]
  81.     index = index + 1 'add 1 to index
  82.     if index < 10 then [insertLoop] 'loop back until we have counted to 10
  83.  
  84.     'There weren't any available slots, inform user
  85.     print "All ten name slots already used!"
  86.     goto [quit]
  87.  
  88. [nameAdded]  'Notify the name add was successful
  89.  
  90.     print yourName$; " has been added to the list."
  91.     goto [askForName]
  92.  
  93. [quit]
  94.  
  95.     end
  96.  
  97. You'll immediately notice the dim names$(10) statement at the start of this 
  98. example.  This tells BASIC to create an array called names$ with space for 
  99. 10 items.  Because the name of the array ends with a $ character, this is an
  100. array of strings.  An array named without a $ character would contain
  101. numeric values.  The first item in an array is referred to by the value 0,
  102. and so an array of 10 items starts with item 0 and ends with item 9 (most
  103. BASICs work like this, but Liberty BASIC will actually number the array from
  104. 0 to 10, giving you eleven items).
  105.  
  106. The real work is done in this part of the code:
  107.  
  108.     index = 0
  109. [insertLoop]
  110.     'check to see if index points to an unused item in the array
  111.     if names$(index) = "" then names$(index) = yourName$ : goto [nameAdded]
  112.     index = index + 1 'add 1 to index
  113.     if index < 10 then [insertLoop] 'loop back until we have counted to 10
  114.  
  115. Here we are using a simple loop to control a search for an empty slot in our 
  116. array names$.  Take a look at this line:
  117.  
  118.     if names$(index) = "" then names$(index) = yourName$ : goto [nameAdded]
  119.  
  120. The  if names$(index) = ""  part of the line uses the value of index to
  121. point to a selected string in the array.  When index is 0, we are looking
  122. at the first item in the array.  When the code loops back and index is 1,
  123. it will be looking at the second item in the array, and so on.
  124.  
  125. In the same line of code, the  names$(index) = yourName$  part is used to
  126. set the item selected by index.  The total effect of the line is to set the
  127. selected item in the array to to be yourName$ if that selected array item
  128. is an empty string.  When this happens it also exits the loop with GOTO
  129. [nameAdded].
  130.  
  131. This is more involved than the technique used in NOARRAY.BAS, but it has the
  132. advantage of size.  Even if we need to manage 1000 items, we only need to
  133. change our code like this (most of the code is left out):
  134.  
  135.     dim names$(1000)
  136.     .
  137.     .
  138. [insertLoop]
  139.     .
  140.     .
  141.     if index < 1000 then [insertLoop] 'loop back until we have counted to 1000
  142.  
  143. If we extend NOARRAY.BAS to manage 1000 names, the resulting program would
  144. have one line for each name slot we needed to keep.  Our program would be
  145. more than 1000 lines long!
  146.  
  147.  
  148. Index Out Of Bounds
  149. ------------------------------------------------------------------------------
  150.  
  151. Try running the following mini program.
  152.  
  153.     'done wrong
  154.     dim names$(10)
  155.     print names$(15)
  156.  
  157. You will get an error message saying 'index out of bounds'.  This means that
  158. you tried to print the contents of an array item that doesn't exist.  If you
  159. dimension the array to make it large enough, it will work.
  160.  
  161.  
  162.     'done right
  163.     dim names$(20)
  164.     print names$(15)
  165.  
  166.  
  167. A similar error will occur if you try to set the item of an array using an
  168. index that is too large.
  169.  
  170.     'done wrong again
  171.     dim names$(10)
  172.     names$(15) = "John Doe"
  173.  
  174.  
  175. Playing the numbers
  176. ----------------------------------------------------------------------------
  177. Numeric arrays work very much like their relative the string array (featured
  178. above in ARRAYS.BAS).  To demonstrate how numeric arrays work, examine this
  179. program that asks for some numbers, adds them up, and calculates the
  180. average value.
  181.  
  182.     'AVERAGE.BAS
  183.     'Accept some numbers from the user, then total and average them
  184.     dim numbers(20)
  185.     print "AVERAGE.BAS"
  186.     print
  187.  
  188.     'loop up to 20 times, getting numbers
  189.     print "Enter up to 20 non-zero values."
  190.     print "A zero or blank entry ends the series."
  191.  
  192. [entryLoop]  'loop around until a zero entry or until index = 20
  193.  
  194.     'get the user's entry
  195.     print "Entry "; index + 1;
  196.     input entry
  197.  
  198.     if entry = 0 then [endSeries]  'quit if entry is zero or blank
  199.  
  200.     index = index + 1       'add one to index
  201.     numbers(index) = entry  'set the specified array item to be entry
  202.     total = total + entry   'add entry to the total
  203.  
  204.     if index = 20 then [endSeries]  'if 20 values were entered, exit loop
  205.  
  206.     goto [entryLoop]  'go back and get another entry
  207.  
  208. [endSeries]  'entries are finished
  209.  
  210.     'Set entryCount to index
  211.     entryCount = index
  212.     if entryCount = 0 then print "No Entries." : goto [quit]
  213.  
  214.     print "Entries completed."
  215.     print
  216.     print "Here are the "; entryCount; " entries:"
  217.     print "-----------------------------"
  218.  
  219.     'This loop displays each entered value in turn.
  220.     'Notice that we re-use the index variable.  It
  221.     'can be confusing to use a new variable for each
  222.     'new loop.    
  223.     index = 0
  224. [displayLoop]
  225.     index = index + 1
  226.     print "Entry "; index; " is "; numbers(index)
  227.     if index < entryCount then [displayLoop]
  228.  
  229.     'Now display the total and average value
  230.     print
  231.     print "The total is "; total
  232.     print "The average is "; total / entryCount
  233.  
  234. [quit]
  235.  
  236.     end
  237.  
  238. Try stepping through this program with the debugger.
  239.  
  240.  
  241. ******************************
  242.       Challenge exercises
  243. ******************************
  244.  
  245. 1) Add code to ARRAYS.BAS after [quit] to display all the names entered.
  246.  
  247.  
  248. 2) There's no reason we can't have more than one array in a BASIC program.
  249. For an example of this, look at the program file FF12.BAS.  Altogether, it
  250. uses no fewer than 18 arrays.  Some of them are string arrays and some are
  251. of the numeric type.
  252.  
  253. Modify the AVERAGE.BAS program so it asks for a name and age for up to 20 
  254. people.  The names should be managed in a string array (remember ARRAYS.BAS?) 
  255. that you'll add to the program.  When the list of entries is displayed, each 
  256. name is to be displayed with its age.  Then the total and average age will be 
  257. displayed after this.  Call the program AGES.BAS.
  258.